package com.oumiao.echarts.service;

import cn.hutool.json.JSONUtil;
import com.oumiao.echarts.config.SocketSessionManager;
import com.oumiao.echarts.config.SocketVO;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Component
public class SocketTestService implements ApplicationRunner {

    Executor executor = Executors.newFixedThreadPool(30);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        SocketVO<String> socketVO = new SocketVO<>();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i+"aaaaaaaaa");
        }
        String jsonStr = JSONUtil.toJsonStr(list);
        socketVO.setTime(new Date());
        socketVO.setData(jsonStr);
        socketVO.setType("test");
        String jsonStr1 = JSONUtil.toJsonStr(socketVO);
        TextMessage textMessage = new TextMessage(jsonStr1);
        while (true) {
            SocketSessionManager.sendToUser("1",textMessage);
        }
    }
}
